//
//  ZMyOrderListOrderChangeView.m
//  DistributorAB
//
//  Created by LZY on 2019/10/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZMyOrderListOrderChangeView.h"
@interface ZMyOrderListOrderChangeView ()
@property (nonatomic, strong) UITextField *tfNumber;//数量
@end

@implementation ZMyOrderListOrderChangeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViewAction];
    }
    return self;
}
#pragma mark - 界面布局方法
- (void)setupViewAction{
    
    self.backgroundColor = [kColorWithHex(0xb1b0af) colorWithAlphaComponent:0.85];
    
    
    UIView *viewCenter = [UIView new];
    viewCenter.backgroundColor = kColorWithHex(0xffffff);
    viewCenter.layer.cornerRadius = 6.0;
    viewCenter.layer.masksToBounds = YES;
    [self addSubview:viewCenter];
    [viewCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(331, 239));
    }];
    
    UILabel *lblNumber = [[UILabel alloc] initWithFrame:CGRectMake(73, 99, 50, 22)];
    lblNumber.text = @"数量:";
    lblNumber.font = [UIFont systemFontOfSize:16];
    lblNumber.textColor = kColorWithHex(0x333333);
    [viewCenter addSubview:lblNumber];
    
    _tfNumber = [[UITextField alloc] initWithFrame:CGRectMake(123, 98, 136, 24)];
    _tfNumber.keyboardType = UIKeyboardTypeNumberPad;
    _tfNumber.font = [UIFont systemFontOfSize:16];
    _tfNumber.textColor = kColorWithHex(0x333333);
    _tfNumber.textAlignment = NSTextAlignmentCenter;
    [viewCenter addSubview:_tfNumber];
    
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(123, 126, 136, 1)];
    viewLine.backgroundColor = kColorWithHex(0x7a7a7a);
    [viewCenter addSubview:viewLine];
    
    UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCancel.frame = CGRectMake(45, 181, 113, 36);
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel setTitleColor:kColorWithHex(0x70be68) forState:UIControlStateNormal];
    btnCancel.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnCancel setBackgroundColor:kColorWithHex(0xffffff)];
    btnCancel.layer.cornerRadius = 4;
    btnCancel.layer.masksToBounds = YES;
    btnCancel.layer.borderColor = kColorWithHex(0x70be68).CGColor;
    btnCancel.layer.borderWidth = 1;
    btnCancel.tag = 200;
    [btnCancel addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewCenter addSubview:btnCancel];
    
    UIButton *btnOk = [UIButton buttonWithType:UIButtonTypeCustom];
    btnOk.frame = CGRectMake(173, 181, 113, 36);
    [btnOk setTitle:@"提交" forState:UIControlStateNormal];
    [btnOk setTitleColor:kColorWithHex(0xffffff) forState:UIControlStateNormal];
    btnOk.titleLabel.font = [UIFont systemFontOfSize:14];
    [btnOk setBackgroundColor:kColorWithHex(0x70be68)];
    btnOk.layer.cornerRadius = 4;
    btnOk.layer.masksToBounds = YES;
    btnOk.tag = 201;
    [btnOk addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewCenter addSubview:btnOk];
    
}

- (void)setOrderModel:(BShopPartModel *)orderModel{
    if (orderModel) {
        _orderModel = orderModel;
    }
}
#pragma mark - 交互方法
//显示
- (void)showView{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    self.frame = window.bounds;
    [window addSubview:self];
}
//关闭
- (void)dismissView{
    [self removeFromSuperview];
    
}
//我知道了
- (void)btnClickAction:(UIButton *)sender{
    NSInteger tag = sender.tag;
    if (tag == 200) {
        [self dismissView];
    }else if (tag == 201){
        NSInteger numberCount = [[_tfNumber.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] integerValue];
        NSInteger lowCount = [self.orderModel.quantity integerValue];
        lowCount = [self.orderModel.quantity integerValue];
        
        if (numberCount < lowCount) {
            NSString *alertStr = [NSString stringWithFormat:@"该产品最低购买%ld件",lowCount];
            ShowToastWithText(alertStr);
        }else{
            kWeakSelf;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObjectIfNotNil:@(numberCount) forKey:@"count"];
            [dict setObjectIfNotNil:self.orderModel.Id forKey:@"id"];
            [NetWorkTool POST:UPDATE_PARTS_ORDER param:dict success:^(id dic) {
                ShowToastWithText(@"更新数量成功");
                [weakSelf dismissView];
                weakSelf.orderModel.count = [NSString stringWithFormat:@"%ld",numberCount];
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(orderUpdateSuccess)]) {
                    [weakSelf.delegate orderUpdateSuccess];
                }
            } other:^(id dic) {
                ShowToastWithText(dic[@"msg"]);
            } fail:^(NSError *error) {
                
            } needUser:YES];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
